<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>CMake - wuzp blog</title><meta name="Description" content="码农"><meta property="og:title" content="CMake" />
<meta property="og:description" content="CMake用法 1.基本指令 PROJECT 指令 PROJECT(projectname [CXX] [C] [Java]) 这个指令定义工程名称，并制定工程支持的语言，支持的语言列表是可以忽略的，默认情况表示支持所有语言。 这" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://wuzpdev.gitee.io/cmake/" /><meta property="og:image" content="https://wuzpdev.gitee.io/logo.png"/><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2022-01-25T13:59:13+08:00" />
<meta property="article:modified_time" content="2022-01-25T13:59:13+08:00" />

<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://wuzpdev.gitee.io/logo.png"/>

<meta name="twitter:title" content="CMake"/>
<meta name="twitter:description" content="CMake用法 1.基本指令 PROJECT 指令 PROJECT(projectname [CXX] [C] [Java]) 这个指令定义工程名称，并制定工程支持的语言，支持的语言列表是可以忽略的，默认情况表示支持所有语言。 这"/>
<meta name="application-name" content="wuzp blog">
<meta name="apple-mobile-web-app-title" content="wuzp blog"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="https://wuzpdev.gitee.io/cmake/" /><link rel="next" href="https://wuzpdev.gitee.io/makefile/" /><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/normalize.css@8.0.1/normalize.min.css"><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.13.0/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.7.2/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "CMake",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/wuzpdev.gitee.io\/cmake\/"
        },"image": [{
                            "@type": "ImageObject",
                            "url": "https:\/\/wuzpdev.gitee.io\/images\/Apple-Devices-Preview.png",
                            "width":  3200 ,
                            "height":  2048 
                        }],"genre": "posts","keywords": "cmake","wordcount":  935 ,
        "url": "https:\/\/wuzpdev.gitee.io\/cmake\/","datePublished": "2022-01-25T13:59:13+08:00","dateModified": "2022-01-25T13:59:13+08:00","license": "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.","publisher": {
            "@type": "Organization",
            "name": "xxxx","logo": {
                    "@type": "ImageObject",
                    "url": "https:\/\/wuzpdev.gitee.io\/images\/avatar.png",
                    "width":  528 ,
                    "height":  560 
                }},"author": {
                "@type": "Person",
                "name": "wuzp"
            },"description": ""
    }
    </script></head>
    <body header-desktop="fixed" header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="wuzp blog"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw'></i></span>wuzp blog</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 所有文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><a class="menu-item" href="/categories/documentation/"> 文档 </a><a class="menu-item" href="/about/"> 关于 </a><a class="menu-item" href="https://gitee.com/wuzpdev" title="Gitee" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw'></i>  </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="wuzp blog"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw'></i></span>wuzp blog</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </div>
                    <a href="javascript:void(0);" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">所有文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><a class="menu-item" href="/categories/documentation/" title="">文档</a><a class="menu-item" href="/about/" title="">关于</a><a class="menu-item" href="https://gitee.com/wuzpdev" title="Gitee" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw'></i></a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw"></i>
            </a></div>
    </div>
</header>
<div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div>
<main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animated flipInX">CMake</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel=" author" class="author"><i class="fas fa-user-circle fa-fw"></i>wuzp</a></span>&nbsp;<span class="post-category">收录于 <a href="/categories/linux/"><i class="far fa-folder fa-fw"></i>Linux</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2022-01-25">2022-01-25</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 935 字&nbsp;
                <i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 2 分钟&nbsp;</div>
        </div><div class="featured-image"><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="images/Desert.jpg"
        data-srcset="/images/Desert.jpg, images/Desert.jpg 1.5x, /images/Desert.jpg 2x"
        data-sizes="auto"
        alt="/images/Desert.jpg"
        title="/images/Desert.jpg" /></div><div class="details toc" id="toc-static"  kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#1基本指令">1.基本指令</a></li>
    <li><a href="#2-基本语法规则">2. 基本语法规则</a></li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><h1 id="cmake用法">CMake用法</h1>
<h2 id="1基本指令">1.基本指令</h2>
<ul>
<li>
<p>PROJECT 指令</p>
<p>PROJECT(projectname [CXX] [C] [Java])</p>
<p>这个指令定义工程名称，并制定工程支持的语言，支持的语言列表是可以忽略的，默认情况表示支持所有语言。</p>
<p>这个指令隐式的定义了两个cmake变量 <projectname>_BINARY_DIR 以及 <projectname>_SOURCE_DIR</p>
<p>同时cmake系统预定义了 PROJECT_BINARY_DIR 和 PROJECT_SOURCE_DIR 两个变量，他们的值跟 <projectname>_BINARY_DIR 和 <projectname>_SOURCE_DIR的值一致，建议直接使用前者，这样即使修改了工程名称，也不会影响这两个变量。</p>
</li>
<li>
<p>SET 指令</p>
<p>SET(VAR [VALUE] [CACHE TYPE DOCSTRING [FORCE]])</p>
<p>用来显式的定义变量</p>
</li>
<li>
<p>MESSAGE 指令</p>
<p>MESSAGE([SEND_ERROR | STATUS | FATAL_ERROR] &ldquo;message to display&rdquo; &hellip;)</p>
<p>用于向终端输出用户定义的消息,包含三种类型:</p>
<ol>
<li>SEND_ERROR 产生错误，生成过程被跳过</li>
<li>STATUS 输出前缀为-的信息</li>
<li>FATAL_ERROR 立即终止所有cmake过程</li>
</ol>
</li>
<li>
<p>ADD_EXECUTABLE 指令</p>
<p>ADD_EXECUTABLE(name, ${SRC_LIST})</p>
<p>该指令会生成一个文件名为name的可执行文件，相关的源文件是SRC_LIST中定义的源文件列表</p>
</li>
<li>
<p>ADD_SUBDIRECTORY 指令</p>
<p>ADD_SUBDIRECTORY(source_dif [binary_dir] [EXCLUDE_FROM_ALL])</p>
<p>该指令用于向当前工程添加存放源文件的子目录，并可以指定中间二进制和目标二进制存放的位置。EXCLUDE_FROM_ALL参数的含义是将这个目录从编译过程中排除。如果不进行bin目录的指定，那么编译结果都将存放在build/src目录</p>
<p>换个地方保存目标二进制
SET(EXECUTE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)</p>
</li>
<li>
<p>ADD_LIBRARY 指令</p>
<p>ADD_LIBRARY(libname [SHARED | STATIC | MODULE] [EXCLUDE_FROM_ALL] source1 source2 &hellip; sourceN)</p>
<ol>
<li>SHARED  动态库</li>
<li>STATIC  静态库</li>
<li>MODULE  在使用dyld的系统有效，如果不支持dyld，则被当做SHARED对待</li>
</ol>
<p>注意：不能通过ADD_LIBRARY同时构建同名的静态库和动态库，可以构建不同名的库，之后通过SET_TARGET_PROPERTIES重命名库</p>
</li>
<li>
<p>INCLUDE_DIRECTORIES 指令</p>
<p>INCLUDE_DIRECTORIES([AFTER | BEFORE] [SYSTEM] dir1 dir2 &hellip;)</p>
<p>这条指令可以用来向工程添加多个特定的头文件搜索路径，路径之间用空格分割，如果路径中包含了空格，可以使用双引号将它括起来，默认的行为是追加到当前的头文件搜索路径的后面，你可以通过两种方式来进行控制搜索路径添加的方式：</p>
<ol>
<li>CMAKE_INCLUDE_DIRECTORIES_BEFORE，通过SET这个cmake 变量为on，可以
将添加的头文件搜索路径放在已有路径的前面。</li>
<li>通过AFTER 或者BEFORE参数，也可以控制是追加还是置前</li>
</ol>
</li>
<li>
<p>LINK_DIRECTORIES 指令</p>
<p>LINK_DIRECTORIES(directory1 directory2 &hellip;)</p>
<p>添加非标准的共享库搜索路径</p>
</li>
<li>
<p>TARGET_LINK_LIBRARIES 指令</p>
<p>TARGET_LINK_LIBRARIES(target library1 &lt;debug | optimized&gt; library2 &hellip;)</p>
<p>为target 添加需要链接的共享库</p>
</li>
</ul>
<h2 id="2-基本语法规则">2. 基本语法规则</h2>
<ul>
<li>变量使用${}方式取值，但是在IF控制语句中是直接使用变量名</li>
<li>指令(参数1 参数2 &hellip;)  参数使用括弧括起，参数之间使用空格或分号分开</li>
<li>指令是大小写无关的，参数和变量时大小写相关的。</li>
</ul>
</div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2022-01-25</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share">
                <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="https://wuzpdev.gitee.io/cmake/" data-title="CMake" data-hashtags="cmake"><i class="fab fa-twitter fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="https://wuzpdev.gitee.io/cmake/" data-hashtag="cmake"><i class="fab fa-facebook-square fa-fw"></i></a><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="https://wuzpdev.gitee.io/cmake/" data-title="CMake" data-image="images/Desert.jpg" data-ralateuid="xxxx"><i class="fab fa-weibo fa-fw"></i></a><a href="javascript:void(0);" title="分享到 人人" data-sharer="renren" data-url="https://wuzpdev.gitee.io/cmake/"><i class="fab fa-renren fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Myspace" data-sharer="myspace" data-url="https://wuzpdev.gitee.io/cmake/" data-title="CMake" data-description=""><i data-svg-src="https://cdn.jsdelivr.net/npm/simple-icons@2.14.0/icons/myspace.svg"></i></a><a href="javascript:void(0);" title="分享到 Blogger" data-sharer="blogger" data-url="https://wuzpdev.gitee.io/cmake/" data-title="CMake" data-description=""><i class="fab fa-blogger fa-fw"></i></a></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw"></i>&nbsp;<a href="/tags/cmake/">cmake</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="/">主页</a></span>
        </section>
    </div>

    <div class="post-nav">
            <a href="/makefile/" class="next" rel="next" title="Makefile">Makefile<i class="fas fa-angle-right fa-fw"></i></a></div>
</div>
</article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2019 - 2022</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">wuzp</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lightgallery.js@1.2.0/dist/css/lightgallery.min.css"><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/smooth-scroll@16.1.3/dist/smooth-scroll.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/autocomplete.js@0.37.1/dist/autocomplete.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/algoliasearch@4.2.0/dist/algoliasearch-lite.umd.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lazysizes@5.2.2/lazysizes.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lightgallery.js@1.2.0/dist/js/lightgallery.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lg-thumbnail.js@1.2.0/dist/lg-thumbnail.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lg-zoom.js@1.2.0/dist/lg-zoom.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sharer.js@0.4.0/sharer.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":10},"comment":{},"lightGallery":{"actualSize":false,"exThumbImage":"data-thumbnail","hideBarsDelay":2000,"selector":".lightgallery","speed":400,"thumbContHeight":80,"thumbWidth":80,"thumbnail":true},"search":{"algoliaAppID":"PASDMWALPK","algoliaIndex":"index.zh-cn","algoliaSearchKey":"b42948e51daaa93df92381c8e2ac0f93","highlightTag":"em","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50,"type":"algolia"}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
